import Vue from 'vue'
import Vuex from 'vuex'

import axios from 'axios'

import createPersistedState from 'vuex-persistedstate'
Vue.use(Vuex)

export default new Vuex.Store({
  plugins: [createPersistedState()],
  state: {
    token: '',
    userInfo: {}
  },
  mutations: {
    updateToken(state, token) {
      state.token = token
    },
    updateUserInfo(state, value) {
      state.userInfo = value
    }
  },
  actions: {
    async getUserInfo(ctx) {
      const { data: res } = await axios.get('/my/userinfo', {
        headers: {
          Authorization: ctx.state.token
        }
      })
      if (res.code != 0) return
      ctx.commit('updateUserInfo', res.data)
    }
  },
})
